home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter08 / RotateHPRPanel.java < prev    next >
Text File  |  2000-06-28  |  3KB  |  124 lines

  1. package applets;
  2.  
  3. import shout3d.*;
  4. import shout3d.core.*;
  5. import shout3d.math.*;
  6.  
  7.  
  8. public class RotateHPRPanel extends Shout3DPanel implements DeviceObserver {
  9.     
  10.     //Node in scene to rotate
  11.     Transform boxTrans;
  12.  
  13.     //pixels on screen
  14.     int pixelStartX;
  15.     int pixelStartY;
  16.     int pixelEndX;
  17.     int pixelEndY;
  18.  
  19.  
  20.     //to store and compute angles
  21.     float [] eulers = new float [3];
  22.     float [] axisAngle = new float [4];
  23.     Quaternion q = new Quaternion();
  24.     
  25.  
  26.     public RotateHPRPanel(Shout3DApplet applet){
  27.         super(applet);
  28.     }
  29.         
  30.         public void customInitialize() {
  31.         
  32.         addDeviceObserver(this,"MouseInput", null);
  33.         boxTrans = (Transform) getNodeByName("Box01");
  34.         
  35.         //get starting rotation value from scene
  36.         axisAngle = boxTrans.rotation.getValue();
  37.         
  38.         //put this rotation into the quaternion
  39.         q.setAxisAngle(axisAngle);
  40.         
  41.         //get it back out as eulers
  42.         q.getEulers(eulers);
  43.         
  44.     
  45.     }
  46.  
  47.  
  48.     protected void finalize()  { 
  49.         removeDeviceObserver(this,"MouseInput");
  50.     }
  51.  
  52.     
  53.     
  54.     public boolean onDeviceInput(DeviceInput di, Object userData) {
  55.         MouseInput mi = (MouseInput) di;
  56.         switch (mi.which){
  57.  
  58.             case MouseInput.DOWN:
  59.                 pixelStartX = mi.x;
  60.                 pixelStartY = mi.y;
  61.                 return true;
  62.  
  63.             case MouseInput.DRAG:
  64.  
  65.               //if left button used
  66.               if(mi.button == 0) {
  67.  
  68.  
  69.                 pixelEndX = mi.x;
  70.                 pixelEndY = mi.y;
  71.  
  72.                 int dragDistanceX = pixelEndX - pixelStartX;
  73.                 int dragDistanceY = pixelEndY - pixelStartY;
  74.                 
  75.                 //convert drag to rotation
  76.                 //at 50 pixels per radian (57.2 degrees)
  77.                 float headingDelta = dragDistanceX/50f;
  78.                 float pitchDelta = dragDistanceY/50f;
  79.                 
  80.                 //compute new heading and pitch
  81.                 eulers[0] = eulers[0] + headingDelta;
  82.                 eulers[1] = eulers[1] + pitchDelta;
  83.  
  84.                 //Convert from Eulers to AxisAngle
  85.                 // using the Quaternion
  86.                 q.setEulers(eulers);
  87.                 q.getAxisAngle(axisAngle);
  88.  
  89.                 //set the Transform to
  90.                 //updated axis angle values
  91.                 boxTrans.rotation.setValue(axisAngle);
  92.  
  93.                 pixelStartX = pixelEndX;
  94.                 pixelStartY = pixelEndY;
  95.                             
  96.                 return true;
  97.                }//end of 0 if
  98.  
  99.               //if right button used
  100.               if(mi.button == 1) {
  101.  
  102.                 pixelEndY = mi.y;
  103.                 int dragDistanceY = pixelEndY - pixelStartY;
  104.                 
  105.                 float rollDelta = dragDistanceY/50f;
  106.                 eulers[2] = eulers[2] + rollDelta;
  107.  
  108.                 q.setEulers(eulers);
  109.                 q.getAxisAngle(axisAngle);
  110.  
  111.                 boxTrans.rotation.setValue(axisAngle);
  112.  
  113.                 pixelStartY = pixelEndY;
  114.                 return true;
  115.                }//end of 1 if
  116.             
  117.         }//end of switch
  118.  
  119.         return false;
  120.  
  121.     }
  122.  
  123.  
  124. }//end of class